﻿using Abp;
using Abp.Castle.Logging.Log4Net;
using Castle.Facilities.Logging;
using Hangfire;
using Hangfire.Redis;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Topshelf;
using Topshelf.Logging;

namespace AVIS.TaskScheduler.ServerNode
{
    public class HangfireService : ServiceControl
    {
        public static AbpBootstrapper _AbpBootstrapper = null;
        private LogWriter _log;
        public HangfireService()
        {
            _log = HostLogger.Get<HangfireService>();
        }

        public bool Start(HostControl hostControl)
        {
            _log.Info("服务启动");

            _AbpBootstrapper = AbpBootstrapper.Create<TaskSchedulerServerNodeModule>();

            // 配置日志
            _AbpBootstrapper.IocManager.IocContainer.AddFacility<LoggingFacility>(
                f => f.UseAbpLog4Net().WithConfig("log4net.config")
            );

            // 初始化
            _AbpBootstrapper.Initialize();

            return true;
        }

        public bool Stop(HostControl hostControl)
        {
            _AbpBootstrapper.Dispose();

            _log.Info("服务停止");
            return true;
        }
    }
}
